package com.eshop.we.interceptor;


import com.eshop.we.business.UserBusiness;
import com.eshop.we.common.ParameterRequest;
import com.eshop.we.constants.BaseConstants;
import com.eshop.we.constants.UserConstants;
import com.eshop.we.user.api.UserService;
import com.eshop.we.user.entity.User;
import com.eshop.we.utils.CookieUtil;
import com.eshop.we.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 通用拦截器
 * @author
 */
public class CommonInterceptor implements HandlerInterceptor{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	 @Autowired
	 private UserBusiness userBusiness;

	 @Autowired
	 private CookieUtil cookieUtil;

	@Override	//执行controller方法前执行
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		logger.info("请求url"+request.getRequestURI());

		if(1==1){
			return  true;
		}
		User user=cookieUtil.getUesrByCookies(request);
		if(user==null){
			setHeaderAndCode(response,BaseConstants.USER_LOGIN_ERROR);
			return false;
		}
		if( request instanceof ParameterRequest){
			ParameterRequest pr = (ParameterRequest)request;
			pr.addParameter(UserConstants.USER_ID, user.getId());
			pr.addParameter(UserConstants.SCHOOL_ID, user.getSchoolId());
		}
		return true;
	}

	@Override	//执行controller方法后执行
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override	//执行controller方法后执行，返回页面前，render渲染（model）
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	protected void setHeaderAndData(HttpServletResponse response,Integer code,String msg,String data)throws IOException {
		String backData = backData( code, msg, data);
		setHeader( response, backData);
	}
	protected void setHeaderAndCode(HttpServletResponse response,Integer code)throws IOException{
		String backData = backData( code, null, null);
		setHeader( response, backData);
	}

	//返回数据
	private String backData(int code,String msg,Object data) throws IOException {
		Map<String, Object> rtn = new HashMap<String, Object>();
		rtn.put("code", code);
		rtn.put("msg", msg);
		rtn.put("data", data);
		String josnStr = JsonUtils.toJson(rtn);
		return josnStr;
	}


	//将数据存入header
	private void setHeader(HttpServletResponse response,String backData){
		response.setHeader("key", backData);
	}




}
